<template>
  <div class="mod-config editViewContainer">
    <split-pane class="splitPane" :min-percent='10' :default-percent='10' split="vertical">
      <template slot="paneL">
        <div class="treeViewContainer">
          <tree-view ref="treeView" :treeData="statusTreeList" :checkChangedHandler="getDataList"></tree-view>
        </div>
      </template>
      <template slot="paneR">
        <table-view ref="view"
                    :searchAttrs="searchAttrs"
                    :primaryKey="primaryKey"
                    :listApiName="listApiName"
                    :deleteApiName="deleteApiName"
                    :saveAuthName="saveAuthName"
                    :deleteAuthName="deleteAuthName"
                    :showAttrs="showAttrs"
                    :exportAuthName="exportAuthName"
                    :exportPlantFunc="exportPlantFunc"
        >
          <template v-slot:header>
            <el-button size="small" type="success"  @click="batchPurchaseModelSuccess">批量完成</el-button>
            <el-button size="small" type="success" @click="downPurchaseModelDetail">导出采购未入库明细</el-button>
          </template>
        </table-view>
      </template>
    </split-pane>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <model-info-add-or-update v-if="modelInfoVisible" ref="modelInfo"></model-info-add-or-update>
    <productinfo-add-or-update v-if="productInfoVisible" ref="productInfo"></productinfo-add-or-update>

    <el-dialog
      title="制定采购合同"
      :visible="purchaseContractVisible"
      width="90%">

      <el-form
        ref="dataForm"
        label-width="120px"
        label-position="left"
        style="margin-left:30px"
      >

        <el-form-item label="供应商" style="width: 400px">
          <el-select v-model="supplierId" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in supplierList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label=采购时间 style="width: 400px">
          <el-date-picker
            v-model="contractTime"
            align="right"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label=交货时间 style="width: 400px">
          <el-date-picker
            v-model="deliveryTime"
            align="right"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>


      </el-form>

      <el-table
        :data="tableListData"
        border
        style="display: inline-block">
        <el-table-column
          prop="id"
          label="订单编号"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-select v-model="scope.row.orderDetailId" clearable filterable placeholder="请选择"  >
              <el-option
                v-for="item in orderDetailList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column
          prop="modelId"
          label="模具名称"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-select v-model="scope.row.modelId" clearable filterable placeholder="请选择">
              <el-option
                v-for="item in modelInfoList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column
          prop="price"
          label="价格"
          width="140"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.price"  placeholder="价格"></el-input-number>
          </template>
        </el-table-column>


        <el-table-column
          prop="successMo"
          label="成模"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.successMo"  placeholder="成模"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column
          prop="primaryMo"
          label="初模"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.primaryMo"  placeholder="初模"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column
          prop="menTou"
          label="闷头"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.menTou"  placeholder="闷头"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column
          prop="louDou"
          label="漏斗"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.louDou"  placeholder="漏斗"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column
          prop="kouZi"
          label="口子"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.kouZi"  placeholder="口子"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column
          prop="xinZi"
          label="芯子"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.xinZi"  placeholder="芯子"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column
          prop="qiTou"
          label="气头"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.qiTou"  placeholder="气头"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column
          prop="jiaZi"
          label="夹子"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.jiaZi"  placeholder="夹子"></el-input-number>
          </template>
        </el-table-column>


        <el-table-column
          prop="qianPian"
          label="钳片"
          align="center"
        >
          <template slot-scope="scope">
            <el-input-number controls-position="right" size="small" :min="0"  v-model="scope.row.qianPian"  placeholder="钳片"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column
          prop="remark"
          label="备注"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            <el-input type="textarea" controls-position="right" size="small" :min="0"  v-model="scope.row.remark" placeholder="备注"></el-input>
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="purchaseContractVisible = false">取 消</el-button>
        <el-button type="primary" @click=savePurchaseContractAndUpdateModelStatus>确 定</el-button>
      </span>

    </el-dialog>
  </div>
</template>


<script>
import tableView from '@/components/tableView'
import basicMixins from '@/utils/commonViewMixins'
import AddOrUpdate from './purchasemodel-add-or-update'
import treeView from '@/components/treeView'
import ModelInfoAddOrUpdate from '@/views/modules/model/modelinfo-add-or-update'
import ProductinfoAddOrUpdate from '@/views/modules/product/productinfo-add-or-update'

import {mRequest} from '@/utils/request'
import {exportHandle, isAuth} from '@/utils'

export default {
  mixins: [basicMixins],
  data() {
    return {
      statusTreeList: [],
      modelInfoVisible: false,
      productInfoVisible: false,
      purchaseContractVisible: false,
      supplierList: [],
      modelInfoList: [],
      orderDetailList: [],
      supplierId: '',
      contractTime: '',
      deliveryTime: '',
      totalAmount: 0,
      remark: '',
      tableListData: [],
      primaryKey: 'id',
      listApiName: 'purchaseApi/listPurchaseModelList',
      deleteApiName: 'purchaseApi/deletePurchaseModel',
      saveAuthName: 'purchase:purchasemodel:save',
      exportAuthName: 'purchase:purchasemodel:delete',
      deleteAuthName: 'purchase:purchasemodel:delete',
      searchAttrs: {
        contractPurchaseId: {
          type: 'autoComplete',
          placeholder: '请输入采购合同号',
          querySearchApiName: 'contractApi/getContractPurchaseInfoList'
        },
        productNo: {
          label: '产品编号',
          placeholder: '请输入产品编号'
        },

/*        productDeviceId: {
          type: 'autoComplete',
          placeholder: '请输入产线',
          querySearchApiName: 'productApi/getProductDeviceListByParent'

        },*/
        supplierId:{
          type: 'autoComplete',
          placeholder: '请输入供应商',
          querySearchApiName: 'supplierApi/getSupplierList'
        },
        dateTimeRange: {
          type: 'date',
          dateType: 'daterange',
          clearable: true
        },
        status: {
          type: 'func',
          dataFunc: () => {
            return this.$refs.treeView.$refs.treeForm.getCheckedKeys()[0]
          }
        }
      },
      showAttrs: {
        supplierName: {
          label: '供应商',
          width: 120
        },
        userName:{
          label: '申请人'
        },
        status: {
          label: '状态',
          type: 'radio',
          items: [
            {value: 0, text: '待采购', tagType: 'info'},
            {value: 2, text: '采购进行中'},
            {value: 3, text: '采购逾期预警',tagType:'danger'},
            {value: 4, text: '采购完成', tagType: 'success'},
            {value: 6, text: '采购暂停', tagType: 'info'},
            {value: 7, text: '入库进行中', tagType: 'warning'},
            {value: 8, text: '入库完成',tagType: 'success'},
            {value: 17, text: '挑选', tagType: 'warning'},
            {value: 18, text: '合格', tagType: 'success'},
            {value: 19, text: '不合格', tagType: 'danger'},
            {value: 20, text: '特采', tagType: 'warning'},
            {value: 99, text: '采购取消', tagType: 'danger'},
          ]
        },
        productNo: {label: '产品名称'},
        modelNo: {
          label: '模具编号'
        },

        clientContractNo: {
          label: '业务合同编号',
          width: 140
        },
/*        productDeviceName: {
          label: '产线',
          width: 100
        },*/
        productWeight: {
          label: '产品重量'
        },
        productVolume: {
          label: '产品容量'
        },
        successMo: {
          label: '成模'
        },
        primaryMo: {
          label: '初模'
        },
        bottomMo: {
          label: '底模'
        },
        menTou: {
          label: '闷头'
        },
        louDou: {
          label: '漏斗'
        },
        kouZi: {
          label: '口子'
        },
        xinZi: {
          label: '芯子'
        },
        qiTou: {
          label: '气头'
        },
        jiaZi: {
          label: '夹子'
        },
        lengQue: {
          label: '冷却'
        },
        chongTou: {
          label: '冲头'
        },
        qianPian: {
          label: '钳片'
        },

        price: {
          label: '价格'
        },
        contractTime: {
          label: '采购时间',
          type: 'date', width: 120
        },
        modelDrawingId: {
          label: '图纸',
          showKey: 'modelNo', type: 'file', width: 140

        },
        purchaseContractNo: {
          label: '采购合同编号',
          width: 140
        },
      }
    }
  },
  components: {
    treeView,
    tableView,
    AddOrUpdate,
    ProductinfoAddOrUpdate,
    ModelInfoAddOrUpdate
  },
  methods: {
    openModelInfoDialog(cb) {
      this.modelInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.modelInfo)) {
          this.$refs.modelInfo.init(null, {status: 1}, cb)
        }
      })
    },
    statusTreeListInit() {
      mRequest('systemApi/getPurchaseStatusList', {}, (res) => {
        this.statusTreeList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    exportPlantFunc(rows){
      this.supplierId = ''
      this.contractTime = ''
      this.deliveryTime = ''
      this.totalAmount = 0
      this.purchaseType = 6
      this.remark = ''
      this.purchaseContractVisible = true
      this.initOrderDetailList()
      this.initModelInfoList()
      this.initSupplierList()
      this.tableListData = rows
    },
    initSupplierList() {
      mRequest('supplierApi/getSupplierList', {}, (res) => {
        this.supplierList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initOrderDetailList() {
      mRequest('orderApi/getOrderDetailList', {}, (res) => {
        this.orderDetailList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    initModelInfoList() {
      mRequest('modelApi/getModelInfoList', {}, (res) => {
        this.modelInfoList = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    openProductInfoDialog(cb) {
      this.productInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.productInfo)) {
          this.$refs.productInfo.init(null, {status: 1}, cb)
        }
      })
    },
    savePurchaseContractAndUpdateModelStatus(){
      mRequest('contractApi/savePurchaseContractAndUpdateModelStatus', {
        'supplierId': this.supplierId,
        'contractTime': this.contractTime,
        'deliveryTime': this.deliveryTime,
        'purchaseType': this.purchaseType,
        'tableListDataModel': this.tableListData
      }, (res => {
        let data = res.data
        if (data && data.code == 0) {
          let contractPurchaseInfo=data.contractPurchaseInfo
          this.$confirm('是否直接导出改计划?', '提示', {
            confirmButtonText: '导出',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            exportHandle('contractApi/getPurchasePartContractExport', {'contractPurchaseId': contractPurchaseInfo.id})
          }).catch(() => {
          })
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.purchaseContractVisible = false
              this.getDataList()
              this.$emit('refreshDataList')
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      }))
    },
    downPurchaseModelDetail() {
      exportHandle('purchaseApi/getPurchaseModelDetailExport', {})
    },
    batchPurchaseModelSuccess() {
      if (this.$refs['view'].dataListSelections.length <= 0) {
        this.$message.error("请选择数据")
        return
      }
      const ids = this.$refs['view'].dataListSelections.map(item => {
        return item[this.primaryKey]
      })
      //batchPurchaseModelSuccess
      console.log(ids)
      mRequest('purchaseApi/batchPurchaseModelSuccess', ids, (res) => {
        if ( res.data.code == 0) {
          this.$message.success("批量完成")
        }

        this.getDataList()
      })
      console.log(ids)

    }
  },
  mounted() {
    this.statusTreeListInit()
  }
}
</script>
